/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tvunetworks.dao.ibatis;

import com.tvunetworks.dao.ProductDao;
import com.tvunetworks.model.Product;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 *
 * @author michael
 */
public class ProductDaoiBatis extends GenericDaoiBatis<Product, String> implements ProductDao {

    public ProductDaoiBatis() {
        super(com.tvunetworks.model.Product.class);
    }

    public Product getProduct(String productId) {
        return (Product) getSqlMapClientTemplate().queryForObject("getProduct", productId);
    }

    public List<Product> getProductListByCategory(String categoryId) {
        return getSqlMapClientTemplate().queryForList("getProductListByCategory", categoryId);
    }

    public List<Product> searchProductList(String keywords) {
        Object parameterObject = new ProductSearch(keywords);
        return getSqlMapClientTemplate().queryForList("searchProductList", parameterObject);
    }

    /* Inner Classes */
    public static class ProductSearch {

        private List keywordList = new ArrayList();

        public ProductSearch(String keywords) {
            StringTokenizer splitter = new StringTokenizer(keywords, " ", false);
            while (splitter.hasMoreTokens()) {
                keywordList.add("%" + splitter.nextToken() + "%");
            }
        }

        public List getKeywordList() {
            return keywordList;
        }
    }
}
